package com.fare.taluo.application.impl.plane.income;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.BaseEntity;
import com.fare.taluo.application.plane.income.OccupRateApplication;
import com.fare.taluo.base.ddd.domain.baseentity.BaseEntity2;
import com.fare.taluo.core.domain.config.channel.AirCompanyEntity;
import com.fare.taluo.core.domain.config.line.OccupRateEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.RandomUtils;
import com.fare.taluo.util.value.NumberUtils;
import com.fare.taluo.vo.core.OccupRateEntityVO;


@Transactional
@Scope("singleton")
@Service("occupRateApplication")
public class OccupRateApplicationImpl implements OccupRateApplication {

	@Override
	public void deleteOccupRateEntity(long id) throws Exception {
		OccupRateEntity occupRate = BaseEntity.get(OccupRateEntity.class, id);
		occupRate.remove();
	}

	@Override
	public void saveOrUpdateOccupRateEntity(OccupRateEntityVO oreVO)
			throws Exception {
		OccupRateEntity occup = new OccupRateEntity();
		if (oreVO != null) {
			occup.setId(oreVO.getId());
			occup.setCompanyCode(oreVO.getCompanyCode());
			occup.setLineCode(oreVO.getLineCode());
			occup.setFlightInfo(oreVO.getFlightInfo());
			occup.setFlightDay(oreVO.getFlightDay());
			occup.setPrice(oreVO.getPrice());
			occup.setOccupRate(oreVO.getOccupRate());
			occup.setIncome(oreVO.getIncome());
		}
		occup.saveOrUpdateOccupRateEntity();
	}

	@Override
	public List<OccupRateEntityVO> findOccupRateByConsdions(String companyId,
			String lineCode, Date flightDay, int page, int pagesize) {
		OccupRateEntity occupRateEntity = new OccupRateEntity();
		List<OccupRateEntity> entityList = occupRateEntity
				.findOccupRateEntityByCondition(companyId, lineCode, flightDay,
						page, pagesize);
		List<OccupRateEntityVO> voList = new ArrayList<OccupRateEntityVO>();

		if (entityList != null) {
			if (entityList.size() > 0) {
				for (OccupRateEntity occupRate : entityList) {
					OccupRateEntityVO vo = new OccupRateEntityVO();
					vo.setId(occupRate.getId());

					vo.setCompanyCode(occupRate.getCompanyCode());
					AirCompanyEntity airCompany = BaseEntity.get(
							AirCompanyEntity.class, occupRate.getCompanyCode());
					vo.setCompanyName(airCompany.getName());

					vo.setLineCode(occupRate.getLineCode());
					// FlightInfoEntity
					// source=BaseEntity.get(SourceEntity.class,
					// adapterVO.getSourceID());
					// vo.setLineName(lineName);

					vo.setFlightInfo(occupRate.getFlightInfo());
					// FlightInfoEntity flightInfo=new FlightInfoEntity();
					// List<FlightInfoEntity>
					// flightList=flightInfo.findByFlightNo(occupRate.getFlightInfo());
					// FlightInfoEntity flightInfo2=flightList.get(0);

					// vo.setFlightName(flightInfo2.getFromCityName()+"("+flightInfo2.getFromCity()+")--"+flightInfo2.getToCityName()+"("+flightInfo2.getToCity()+")");
					vo.setFlightDay(occupRate.getFlightDay());
					vo.setPrice(occupRate.getPrice());
					vo.setOccupRate(occupRate.getOccupRate());

					vo.setIncome(occupRate.getIncome());

					voList.add(vo);
				}
			}
		}
		return voList;
	}

	@Override
	public int getTotalOccupRateListByCodition(String companyId,
			String fromCity,String toCity, Date flightDay) {
		OccupRateEntity occup = new OccupRateEntity();
		return occup.getTotalOccupRateListByCodition(companyId, fromCity,toCity,
				flightDay);
	}
	public OccupRateEntityVO findOccupRateEntityVO(String flightno,String flightDay){
		OccupRateEntity occup = new OccupRateEntity();
		OccupRateEntityVO vo = new OccupRateEntityVO();
		Date date;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date = sdf.parse(flightDay);
			occup = occup.findOccupRateEntityByCondition(flightno, date);
			if (occup != null) {
				vo.setId(occup.getId());
				vo.setFlightInfo(occup.getFlightInfo());
				vo.setFlightDay(occup.getFlightDay());
				vo.setPrice(occup.getPrice());
				vo.setOccupRate(occup.getOccupRate());
				vo.setIncome(occup.getIncome());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return vo;
	}
	public List<OccupRateEntityVO> findOccupRateCondition2(String linkCode, String flightDay){
		OccupRateEntity occupRateEntity = new OccupRateEntity();
		List<OccupRateEntity> entityList = occupRateEntity.findOccupRateCondition2(linkCode,flightDay);
		List<OccupRateEntityVO> voList = new ArrayList<OccupRateEntityVO>();

		if (entityList != null) {
			if (entityList.size() > 0) {
				for (OccupRateEntity occupRate : entityList) {
					OccupRateEntityVO vo = new OccupRateEntityVO();
					vo.setId(occupRate.getId());

					vo.setCompanyCode(occupRate.getCompanyCode());
					AirCompanyEntity airCompany = BaseEntity.get(
							AirCompanyEntity.class, occupRate.getCompanyCode());
					vo.setCompanyName(airCompany.getName());

					vo.setLineCode(occupRate.getLineCode());
					// FlightInfoEntity
					// source=BaseEntity.get(SourceEntity.class,
					// adapterVO.getSourceID());
					// vo.setLineName(lineName);

					vo.setFlightInfo(occupRate.getFlightInfo());

					vo.setFlightDay(occupRate.getFlightDay());
					vo.setPrice(occupRate.getPrice());
					vo.setOccupRate(occupRate.getOccupRate());

					vo.setIncome(occupRate.getIncome());
					vo.setIncome2(Double.valueOf(occupRate.getIncome()+""));
					vo.setOccupRate2(Double.valueOf(occupRate.getOccupRate()+""));
					voList.add(vo);
				}
			}
		}
		return voList;
	}
	public List<OccupRateEntityVO> findOccupRateEntityByCondition(String companyCode, String flightDay){
		OccupRateEntity occupRateEntity = new OccupRateEntity();
		List<OccupRateEntity> entityList = occupRateEntity.findOccupRateEntity(companyCode,flightDay);
		List<OccupRateEntityVO> voList = new ArrayList<OccupRateEntityVO>();

		if (entityList != null) {
			if (entityList.size() > 0) {
				for (OccupRateEntity occupRate : entityList) {
					OccupRateEntityVO vo = new OccupRateEntityVO();
					vo.setId(occupRate.getId());

					vo.setCompanyCode(occupRate.getCompanyCode());
					AirCompanyEntity entity=new AirCompanyEntity();
					AirCompanyEntity airCompany = entity.findCompanNameByCode(occupRate.getCompanyCode());
					vo.setCompanyName(airCompany.getName());

					vo.setLineCode(occupRate.getLineCode());
					// FlightInfoEntity
					// source=BaseEntity.get(SourceEntity.class,
					// adapterVO.getSourceID());
					// vo.setLineName(lineName);

					vo.setFlightInfo(occupRate.getFlightInfo());

					vo.setFlightDay(occupRate.getFlightDay());
					vo.setPrice(occupRate.getPrice());
					vo.setOccupRate(occupRate.getOccupRate());

					vo.setIncome(occupRate.getIncome());
					vo.setIncome2(Double.valueOf(occupRate.getIncome()+""));
					vo.setOccupRate2(Double.valueOf(occupRate.getOccupRate()+""));
					voList.add(vo);
				}
			}
		}
		return voList;
	}

	public List<OccupRateEntityVO> findOccupRateEntityByCondition(
			String flightinfo, Date beginFlightDay, Date endFlightDay)  {
		List<OccupRateEntityVO> volist=new ArrayList<OccupRateEntityVO>();
		int days = DateUtils.intervalDays(beginFlightDay, endFlightDay);
		List<Double> rdValues = RandomUtils.randomDoubles(days, 99, 30);
		OccupRateEntityVO vo = null;
		for(Double d : rdValues){
			vo=new OccupRateEntityVO();
			vo.setOccupRate(new BigDecimal(NumberUtils.round(d, 2)));
			
			volist.add(vo);
		}
		return volist;
	}

	@Override
	public List<OccupRateEntityVO> findOccupRateByCode(String companyId,
			String formCity, String toCity, Date date, int page, int pagesize) {
		OccupRateEntity occupRateEntity = new OccupRateEntity();
		List<OccupRateEntity> entityList = occupRateEntity.findOccupRateByCode(companyId, formCity, toCity, date, page, pagesize);
		List<OccupRateEntityVO> voList = new ArrayList<OccupRateEntityVO>();

		if (entityList != null) {
			if (entityList.size() > 0) {
				for (OccupRateEntity occupRate : entityList) {
					OccupRateEntityVO vo = new OccupRateEntityVO();
					vo.setId(occupRate.getId());

					vo.setCompanyCode(occupRate.getCompanyCode());
					AirCompanyEntity airCompany = BaseEntity.get(
							AirCompanyEntity.class, occupRate.getCompanyCode());
					vo.setCompanyName(airCompany.getName());

					vo.setLineCode(occupRate.getLineCode());
					// FlightInfoEntity
					// source=BaseEntity.get(SourceEntity.class,
					// adapterVO.getSourceID());
					// vo.setLineName(lineName);

					vo.setFlightInfo(occupRate.getFlightInfo());
					// FlightInfoEntity flightInfo=new FlightInfoEntity();
					// List<FlightInfoEntity>
					// flightList=flightInfo.findByFlightNo(occupRate.getFlightInfo());
					// FlightInfoEntity flightInfo2=flightList.get(0);

					// vo.setFlightName(flightInfo2.getFromCityName()+"("+flightInfo2.getFromCity()+")--"+flightInfo2.getToCityName()+"("+flightInfo2.getToCity()+")");
					vo.setFlightDay(occupRate.getFlightDay());
					vo.setPrice(occupRate.getPrice());
					vo.setOccupRate(occupRate.getOccupRate());

					vo.setIncome(occupRate.getIncome());
					vo.setFormCity(occupRate.getFormCity());
					vo.setToCity(occupRate.getToCity());
					voList.add(vo);
				}
			}
		}
		return voList;
	}
	

}
